//
//  MoreViewController.m
//  Travelmemory
//
//  Created by lanou3g on 16/3/30.
//  Copyright © 2016年 ABCD. All rights reserved.
//

#import "MoreViewController.h"
#import "StoryCollectionCell.h"
#import "RecommendModel.h"
#import "NextImageViewController.h"
#import "TravelHelper.h"
#import "UIImageView+WebCache.h"
#import "NextImageViewController.h"

@interface MoreViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collecionView;

/** 全部Model */
@property (nonatomic, strong) NSMutableArray *allModelArray;

@end

@implementation MoreViewController

#pragma mark - 懒加载
- (UICollectionView *)collecionView {
    if (!_collecionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(self.view.frame.size.width / 2 - 15, 200);
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.collecionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 5;
        self.collecionView.backgroundColor = [UIColor orangeColor];
        self.collecionView.delegate = self;
        self.collecionView.dataSource = self;
        [self.view addSubview:self.collecionView];
    }
    return _collecionView;
}
- (NSMutableArray *)allModelArray {
    if (!_allModelArray) {
        self.allModelArray = [NSMutableArray array];
    }
    return _allModelArray;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.allModelArray  = [NSMutableArray array];
    
    self.navigationItem.title = @"精选故事";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(dismissAction)];
    // 注册
    [self.collecionView registerNib:[UINib nibWithNibName:@"StoryCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"story"];
    
//    [GiFHUD setGifWithImageName:@"pika.gif"];
//    [GiFHUD show];
    // 解析数据
    [self resolveData:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0"];
    

    

    
}

#pragma mark - dismiss
- (void)dismissAction {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 解析数据
- (void)resolveData:(NSString *)urlStr {
    
    [[TravelHelper defaultTravelHelper] get:urlStr params:nil success:^(id responseObj) {
        for (NSDictionary *dict in responseObj[@"data"][@"hot_spot_list"]) {
            //NSLog(@"%@", dict);
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            
            // 用户字典
            [model setValuesForKeysWithDictionary:dict[@"user"]];
            [model setValue:dict[@"user"][@"name"] forKey:@"user_name"];
            // poi
            if ([dict[@"poi"] isKindOfClass:[NSDictionary class]]) {
                [model setValue:dict[@"poi"][@"name"] forKey:@"poi_name"];
                [model setValuesForKeysWithDictionary:dict[@"poi"]];
            }
            [self.allModelArray addObject:model];
        }
//        [GiFHUD dismiss];
//        [self.collecionView.header endRefreshing];
//        [self.collecionView.footer endRefreshing];
        [self.collecionView reloadData];
        
    } failure:^(NSError *error) {
        
        UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"系统提示" message:@"网络链接失败,请查看网络连接" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        
        [view show];
    }];
    
}


#pragma mark - collectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.allModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    RecommendModel *model = self.allModelArray[indexPath.row];
    StoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"story" forIndexPath:indexPath];
    
  
    [cell.HeaderImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_image_s] placeholderImage:[UIImage imageNamed:@"11"] options:SDWebImageRefreshCached];
    cell.ContentLabel.text = model.text;
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar_l] placeholderImage:[UIImage imageNamed:@"11"] options:SDWebImageRefreshCached];
 
    cell.NameLabel.text = model.user_name;
    if (model.poi_name != NULL) {
        cell.addressLabel.text = [NSString stringWithFormat:@"%@ %@", model.poi_name, model.address];
    }else {
        cell.addressLabel.text = @"";
    }
    return cell;
}
#pragma mark - collectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //NSLog(@"点击了第%zd个cell", indexPath.row);
    RecommendModel *model = self.allModelArray[indexPath.row];
    NextImageViewController *nextVC = [[NextImageViewController alloc] init];
    nextVC.model = model;
    [self showViewController:nextVC sender:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
